package com.ruoyi.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.domain.Document;
import com.ruoyi.domain.DocumentCategory;
import com.ruoyi.mapper.DocumentMapper;
import com.ruoyi.platform.service.impl.BaseServiceImpl;
import com.ruoyi.platform.utils.StringUtils;
import com.ruoyi.service.IDocumentCategoryService;
import com.ruoyi.service.IDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 文档Service业务层处理
 *
 * @author tp
 * @date 2022-11-02
 */
@Service
public class DocumentServiceImpl extends BaseServiceImpl<DocumentMapper, Document> implements IDocumentService {

    @Autowired
    private DocumentMapper documentMapper;

    @Autowired
    private IDocumentCategoryService documentCategoryService;

    /**
     * 查询文档
     *
     * @param id 文档主键
     * @return 文档
     */
    @Override
    public Document selectDocumentById(Integer id) {
        return documentMapper.selectDocumentById(id);
    }

    /**
     * 查询文档列表
     *
     * @param document 文档
     * @return 文档
     */
    @Override
    public List<Document> selectDocumentList(Document document) {
        return documentMapper.selectDocumentList(document);
    }

    /**
     * 新增文档
     *
     * @param document 文档
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDocument(Document document) {
        Assert.isTrue(documentMapper.selectDocumentByName(document.getName()).size() == 0, "失败，文档名称已存在");
        int flag = documentMapper.insert(document);
        DocumentCategory documentCategory = new DocumentCategory();
        documentCategory.setDocumentId(document.getId().longValue());
        documentCategory.setCategoryName(document.getName());
        documentCategory.setParentId(document.getCategoryId());
        documentCategory.setIsRoot(0);
        documentCategoryService.insertDocumentCategory(documentCategory);
        return flag;
    }

    /**
     * 修改文档
     *
     * @param document 文档
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDocument(Document document) {
        Document oldDocument = documentMapper.selectDocumentById(document.getId());
        DocumentCategory category = documentCategoryService.getOne(new LambdaQueryWrapper<DocumentCategory>()
                .eq(DocumentCategory::getDocumentId, oldDocument.getId()));
        DocumentCategory documentCategory = new DocumentCategory();
        documentCategory.setId(category.getId());
        documentCategory.setParentId(document.getCategoryId());
        documentCategory.setCategoryName(document.getName());
        documentCategoryService.updateDocumentCategory(documentCategory);
        return documentMapper.updateById(document);
    }

    /**
     * 批量删除文档
     *
     * @param ids 需要删除的文档主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDocumentByIds(Integer[] ids) {
        for (Integer id : ids) {
            Document document = documentMapper.selectDocumentById(id);
            documentCategoryService.deleteCategoryByDocName(document.getName());
        }
        return documentMapper.deleteDocumentByIds(ids);
    }

    /**
     * 删除文档信息
     *
     * @param id 文档主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDocumentById(Integer id) {
        return documentMapper.deleteDocumentById(id);
    }

    @Override
    public List<Document> getDocumentListByName(String name) {
        if (StringUtils.isNull(name)) {
            name = "";
        }
        return documentMapper.selectDocumentByName(name);
    }

    @Override
    public List<Document> querySearchDocument(String name) {
        return list(new LambdaQueryWrapper<Document>().like(Document::getName,name));
    }


}
